package mo.leetcode_top;

import java.util.HashSet;
import java.util.Set;

public class L128 {

    public int longestConsecutive(int[] nums) {
        Set<Integer> numSet = new HashSet<>();
        for (int num : nums) {
            numSet.add(num);
        }
        int longest  = 0;
        for (int num : nums) {
            //不存在比当前元素小的元素
            if (!numSet.contains(num-1)) {
                int curNum = num;
                int curContinue = 1;
                while (numSet.contains(curNum+1)) {
                    curContinue++;
                    curNum++;
                }
                longest = Math.max(longest, curContinue);
                //连续的长度超过一半，就是最大连续长度
                if (longest >= nums.length / 2 + 1) {
                    break;
                }
            }
        }
        return longest;
    }

    public static void main(String[] args) {
        L128 test = new L128();
        System.out.println(test.longestConsecutive(new int[]{0,3,7,2,5,8,4,6,0,1}));
    }

}
